package xyz.bali16.module.nav.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.module.nav.entity.Nav;
import xyz.bali16.module.nav.mapper.NavMapper;
import xyz.bali16.module.nav.model.NavAddParam;
import xyz.bali16.module.nav.model.NavPageParam;
import xyz.bali16.module.nav.model.NavUpdateParam;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class NavServiceImpl extends ServiceImpl<NavMapper, Nav> implements NavService {
    private final NavMapper navMapper;

    public NavServiceImpl(NavMapper navMapper) {
        this.navMapper = navMapper;
    }

    @Override
    public IPage<Nav> getPage(NavPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getName(),param.getUrl());
    }

    @Override
    public void addNav(NavAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "NavAddParam 参数不能为为空");
        Nav nav = new Nav();
        BeanUtils.copyProperties(param, nav);
        System.out.println(nav);
        save(nav);
    }

    @Override
    public void updateNav(NavUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "NavAddParam 参数不能为为空");
        Nav nav = new Nav();
        BeanUtils.copyProperties(param, nav);
        updateById(nav);
    }

    @Override
    public void updateStatus(String navId, Integer status) throws Exception {
        Preconditions.checkNotNull(navId, "导航id不能为空");
        if (status == null) {
            return;
        }
        Nav nav = new Nav();
        nav.setId(navId);
        nav.setStatus(status);
        updateById(nav);
    }

    @Override
    public void delete(String navId) throws Exception {
        if (navId == null) {
            return;
        }
        removeById(navId);

    }
}
